﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HWmanagementClient.HWManager;
using HWmanagementClient.Codes;
using System.IO;
using HWmanagementClient.Models;

namespace HWmanagementClient.Controllers
{
    public class StudentController : Controller
    {
        protected override void ExecuteCore()
        {
            try
            {
                ViewBag.Rank = HWmanagementClient.Codes.Site.GetAuthUserRank(Request, User);
            }
            catch
            {
                ViewBag.Rank = -1;
            }
            base.ExecuteCore();
        }


        //
        // GET: /Student/

        private StudentHomeworks loadhomeworks()
        {
            ManagerClient proxy = new ManagerClient();
            var hwk = proxy.getSubjects(Site.GetAuthUserCode(Request, User));


            StudentHomeworks model = new StudentHomeworks()
            {
                Subjects = hwk.Select(
                    (sm) => SubjectClientModel.fromWCFmodel(sm)
                    ).ToList()
            };

            foreach (var item in model.Subjects)
            {
                item.courses = proxy.getCourses(item.neptun).ToList();
            }

            return model;
        }

        public ActionResult Index()
        {
            return View(loadhomeworks());
        }

        public ActionResult Grades()
        {
            return View(loadhomeworks());
        }


        [HttpPost]
        public ActionResult GetHomeworks(int id)
        {
            ManagerClient proxy = new ManagerClient();
            var hwk = proxy.getStudentHomeworks(Site.GetAuthUserCode(Request, User), id);

            StudentHomeworks model = new StudentHomeworks();
            model.Grades = new List<GradeModel>();

            foreach (var item in hwk)
            {
                var gm = GradeModel.fromBase(GradeModel.fromWCFmodel(item));
                try
                {
                    var grade = proxy.viewResult(item.id, Site.GetAuthUserCode(Request, User));
                    gm.grade = grade.grade;
                    gm.comment = grade.comment;
                }
                catch
                {
                    gm.grade = 0;
                    gm.comment = "";
                }
                
                model.Grades.Add(gm);
            }

            return Json(model);
        }

        [HttpPost]
        public ActionResult GetGrade(int id)
        {
            ManagerClient proxy = new ManagerClient();
            var hwk = proxy.getStudentHomeworks(Site.GetAuthUserCode(Request, User), id);

            StudentHomeworks model = new StudentHomeworks();
            model.Grades = new List<GradeModel>();

            foreach (var item in hwk)
            {
                proxy = new ManagerClient();
                var gm = GradeModel.fromBase(GradeModel.fromWCFmodel(item));
                try
                {
                    var grade = proxy.viewResult(item.id, Site.GetAuthUserCode(Request, User));
                    gm.grade = grade.grade;
                    gm.comment = grade.comment;
                }
                catch
                {
                    gm.grade = 0;
                    gm.comment = "";
                }
                
                proxy = new ManagerClient();
                try
                {
                    gm.fileid = proxy.getFiles(item.id, Site.GetAuthUserCode(Request, User), true).Last().id;
                }
                catch
                {

                }

                model.Grades.Add(gm);
            }

            return Json(model);
        }


        //THIS IS THE HOMEWORK UPLOADING!!!!!!!!!!!!!!!!!!!!!!!
        [HttpPost]
        public ActionResult Grades(HttpPostedFileBase file, FormCollection formCollection)
        {
            var homew = int.Parse(formCollection["homew"]);
            var neptun = Site.GetAuthUserCode(Request, User);
            var version = string.Format("{0}{1}{2}{3}{4}{5}",
                DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

            string path = "";

            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = "" + version + "_hw" + homew + "_" + Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                DirectoryInfo dir = new DirectoryInfo(Path.Combine(Server.MapPath("~/App_Data/HWmanager/uploads/"+neptun)));
                if (!dir.Exists)
                {
                    dir.Create();
                } 
                path = Path.Combine(Server.MapPath("~/App_Data/HWmanager/uploads/"+neptun), fileName);
                try
                {
                    file.SaveAs(path);
                    ViewBag.Succeeded = true;
                }
                catch
                {
                    ViewBag.Succeeded = false;
                    return View(loadhomeworks());
                }
                
            }

            //TODO
            //ITT BE KELL ADNI A HÁZIT ÉS A VISSZAADOTT ÉRTÉKBEN MEGNÉZNI HOGY SIKERÜLT-e
            ManagerClient proxy = new ManagerClient();

            FileModel fm = new FileModel()
            {
                name = path
            };

            proxy.PostHomework(homew, neptun, fm);

            ViewBag.Succeeded = true;

            // redirect back to the index action to show the form once again
            return View(loadhomeworks());
        }

    }
}
